package org.demo.service;

import org.demo.client.UserClient;
import org.demo.entity.Order;
import org.demo.entity.User;
import org.demo.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author tuk
 * @Date 2025/5/27 上午11:30
 */

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserClient userClient;




    public Order queryById(Long id) {
        //1.查询订单
        Order order = orderMapper.findById(id);

        //2.远程查询User
        //2.1 设置url地址
//        String url = "http://localhost:8082/user/" + order.getUserId();
//        String url = "http://userService/user/" + order.getUserId();
        //2.2发起调用
//        User user = restTemplate.getForObject(url,User.class);
        User user = userClient.findById(order.getUserId());

        //3.存入user对象中
        order.setUser(user);
        //返回
        return order;
    }
}
